<template>
    <div class="qingwu">
        <div class="admin_table_page_title">水表设备管理</div>
        <div class="unline underm"></div>

        <estate-search :searchConfig="searchConfig" @searchParams="search">
            <a-form-model-item label="分类">
                <a-select default-value="请选择分类" @change="handleChange">
                    <a-select-option v-for="(v,k) in classify" :key="v.id" :value="v.id">
                        {{ v.title }}
                    </a-select-option>
                </a-select>
            </a-form-model-item>
        </estate-search>

        <div class="admin_table_handle_btn">
            <a-button @click="$router.push('/Estate/water_meter/form')" type="primary" icon="plus">添加</a-button>
            <a-button class="admin_delete_btn" type="danger" icon="delete" @click="del">批量删除</a-button>
        </div>
        <div class="admin_table_list">
            <a-table :columns="columns" :data-source="list" :pagination="false" :loading="table_loading"
                     :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" row-key="id">
                <span slot="fee" slot-scope="rows" @click="$router.push('/Estate/water_meter/detail/'+rows.id+'/'+rows.code+'/'+rows.community+'/'+rows.tableAddress)">
                    <a-tag color="orange">{{ rows.code }}</a-tag>

                </span>
                <span slot="base_info" slot-scope="rows">
                    区域:<a-tag color="orange">{{ rows.community_name }}</a-tag><br>
                   采集器: <a-tag color="green">{{ rows.collector }}</a-tag><br/>
                   倍率: <a-tag color="green">{{ rows.mult }}</a-tag>
                    <!--                    <a-tag  color="orange"></a-tag>-->

                </span>
                <span slot="plan_show" slot-scope="rows">
                    缴费方案:<a-tag color="orange">{{ rows.charge }}</a-tag><br>
                   报警方案: <a-tag color="green">{{ rows.alarm }}</a-tag><br/>
                    <!--                    <a-tag  color="orange"></a-tag>-->

                </span>
                <span slot="status" slot-scope="rows">
                       <a-tag v-if="rows.status==1" color="green">启用</a-tag>
                    <a-tag v-if="rows.status==2" color="red">禁用</a-tag>
                    <!--                    <a-tag  color="orange"></a-tag>-->

                </span>
            </a-table>
            <div class="admin_pagination" v-if="total>0">
                <a-pagination v-model="params.page" :page-size.sync="params.per_page" :total="total" @change="onChange"
                              show-less-items/>
            </div>
        </div>
    </div>
</template>

<script>
import estateSearch from '@/components/estate/search'

export default {
    components: {estateSearch,},
    props: {},
    data() {
        return {
            params: {
                page: 1,
                per_page: 30,
                type:2,
            },
            total: 0, //总页数
            community: [],
            collector: [],
            alarm: [],
            plan: [],
            searchConfig: [
                {label: '资产编号', name: 'code', type: 'text'},
                {label: '表地址', name: 'tableAddress', type: 'text'},
                {
                    label: '区域', name: 'community', type: 'select', data: [{label: '全部', value: ''}]
                },
                {label: '采集器', name: 'collector', type: 'select',data: [{label:'全部',value:''}]},
                {label: '收费方案', name: 'plan',  type: 'select',data: [{label:'全部',value:''}]},
                {label: '报警方案', name: 'alarm',  type: 'select',data: [{label:'全部',value:''}]},
                {label: '创建时间', name: 'created_at', type: 'date_picker'},
            ],
            selectedRowKeys: [], // 被选择的行
            columns: [
                {title: 'ID', fixed: 'left', dataIndex: 'id'},
                {title: '水名称', dataIndex: 'name'},
                {title: '资产编号', key: 'id', scopedSlots: {customRender: 'fee'}},
                {title: '表地址', dataIndex: 'tableAddress'},
                {title: '水量', dataIndex: 'electricity'},
                {title: '基本信息', key: 'id', scopedSlots: {customRender: 'base_info'}},
                // {title:'品牌',key:'pay_type',scopedSlots: {customRender: 'pay'}},
                {title: '方案', key: "id", scopedSlots: {customRender: 'plan_show'}},
                {title: '状态', key: 'id', scopedSlots: {customRender: 'status'}},
                {title: '操作', key: 'id', fixed: 'right', scopedSlots: {customRender: 'action'}},
            ],
            list: [],
            count: [],
            table_loading: true,
        };
    },
    watch: {},
    computed: {},
    methods: {
        search(params) {
            let page = this.params.page;
            let per_page = this.params.per_page;
            this.params = params;
            this.params.page = page;
            this.params.per_page = per_page;
            this.onload();
        },
        // 选择框被点击
        onSelectChange(selectedRowKeys) {
            this.selectedRowKeys = selectedRowKeys;
        },
        // 删除
        del() {
            if (this.selectedRowKeys.length == 0) {
                return this.$message.error('未选择数据.');
            }
            this.$confirm({
                title: '你确定要删除选择的数据？',
                content: '确定删除后无法恢复.',
                okText: '是',
                okType: 'danger',
                cancelText: '取消',
                onOk: () => {
                    let ids = this.selectedRowKeys.join(',');
                    this.$delete(this.$api.estateWaterMeter + '/' + ids).then(res => {
                        if (res.code == 200) {
                            this.onload();
                            this.$message.success('删除成功');
                        } else {
                            this.$message.error(res.msg)
                        }
                    });

                },
            });
        },
        // 选择分页
        onChange(e) {
            this.params.page = e;
            this.onload();
        },
        onload() {
            console.log(this.params,"v参数");
            this.$get(this.$api.estateWaterMeter, this.params).then(res => {
                this.list = res.data.data;
                this.count = res.data.count;
                this.total = res.data.total;
                this.table_loading = false;
            });
            this.$get(this.$api.estateGetCommunity, this.params).then(res => {
                this.setCommunity(res.data.community_arr);
                this.setCollector(res.data.collector_arr);
                this.setPlan(res.data.plan_arr);
                this.setAlarm(res.data.alarm_arr);
                this.table_loading = false;
            });
        },
        setCommunity(data) {
            let list = [];
            data.forEach(item => {

                list.push({label: item.label, value: item.value})
            })
            this.searchConfig.forEach(item => {
                if (item.name == 'community') {
                    item.data = item.data.concat(list)
                }
            })
        },
        setCollector(data) {
            let list = [];
            data.forEach(item => {

                list.push({label: item.label, value: item.value})
            })
            this.searchConfig.forEach(item => {
                if (item.name == 'collector') {
                    item.data = item.data.concat(list)
                }
            })
        },
        setPlan(data) {
            let list = [];
            data.forEach(item => {

                list.push({label: item.label, value: item.value})
            })
            this.searchConfig.forEach(item => {
                if (item.name == 'plan') {
                    item.data = item.data.concat(list)
                }
            })
        },
        setAlarm(data) {
            let list = [];
            data.forEach(item => {

                list.push({label: item.label, value: item.value})
            })
            this.searchConfig.forEach(item => {
                if (item.name == 'alarm') {
                    item.data = item.data.concat(list)
                }
            })
        },
    },
    created() {
        this.onload();
    },
    mounted() {
    }
};
</script>
<style lang="scss" scoped>

</style>
